/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.templates.client;

import org.nuxeo.gwt.habyt.invocation.client.ObjectRef;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public class RootTemplate extends TemplatePanel {

    private static TemplatePanel root;

    public static TemplatePanel get() {
        return root;
    }

    public static TemplatePanel loadTemplate(ObjectRef template) {
        return loadTemplate(template, null);
    }

    public static TemplatePanel loadTemplate(ObjectRef template,
            AsyncCallback<TemplatePanel> cb) {
        if (root != null) {
            RootPanel.detachNow(root);
        }
        root = new TemplatePanel();
        root.load(template, cb);
        return root;
    }

    public static TemplatePanel wrap(String id) {
        return wrap(id, null);
    }

    public static TemplatePanel wrap(String id, AsyncCallback<TemplatePanel> cb) {
        return wrap(Document.get().getElementById(id), cb);
    }

    public static TemplatePanel wrap(Element element) {
        return wrap(element, null);
    }

    public static TemplatePanel wrap(Element element,
            AsyncCallback<TemplatePanel> cb) {
        if (root != null) {
            RootPanel.detachNow(root);
        }
        root = TemplatePanel.wrap(element, cb);
        return root;
    }

}
